// pages/mine/withdrawal/withdrawal.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../../api/index'),
    utils = require('../../../utils/util');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        canCash: false, // 是否可提现
        cashNum: 0, // 提现金额
        canCashNum: 0, // 可提现金额
        toast: {
            visible: false,
            text: ''
        },
        loading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getBalance();
    },

    // 获取我的可提现金额
    async getBalance() {
        try {
            let res = await api.balance.getMyBalance(),
                data = res.data.ResData || 0;
            this.setData({
                balance: data,
                canCashNum: data.Amount
            })
            console.log(res, '可提现金额')
        } catch (e) {
            console.log(e, '获取可提现金额失败')
        }
    },

    getCashNum(e) {
        let cashNum = e.detail.value;
        this.setData({
            cashNum
        })
    },

    checkNum() {
        console.log(this.data.cashNum, 'cashnum')
        let cashNum = this.data.cashNum,
            canCashNum = this.data.canCashNum,
            exp = /^(-[1-9]\d*|[1-9]\d*)$/;

        // 不是数字
        if (!exp.test(cashNum)) {
            this.setData({
                toast: {
                    visible: true,
                    text: '请输入数字'
                }
            });
            return
        }

        // 负数或小于100
        if (cashNum < 0 || cashNum < 100) {
            this.setData({
                toast: {
                    visible: true,
                    text: '请输入大于100的数字'
                }
            });
            return
        }

        // 是否小于等于可提现金额
        if (canCashNum < cashNum) {
            this.setData({
                toast: {
                    visible: true,
                    text: '余额不足'
                }
            });
            return
        }

        this.setData({
            canCash: true
        })
    },

    async withdrawalCash() {
        if (this.data.loading) return;
        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '正在努力提现中',
        })

        let obj = {
            amount: this.data.cashNum,
            remark: ""
        }

        try {
            let res = await api.balance.withdrawal(obj);
            console.log(res, '提现')

            this.setData({
                toast: {
                    visible: true,
                    text: '提现成功'
                },
                loading: false
            })
            this.getBalance();
        } catch (e) {
            console.log(e, '提现失败')
            this.setData({
                toast: {
                    visible: true,
                    text: e.data.ErrMsg || '提现失败'
                },
                loading: false
            })
        } finally {
            wx.hideLoading()
        }
        console.log(this.data.cashNum, 'cashnum')
    }
})